/*
*  Copyright (C) 2009  Peter Kist & Jan Ripke
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "SettingsMenu.h"

#include <Util/Logger.h>
#include <Modules/Gfx/Ogre/Types.h>
#include <Modules/Gfx/Ogre/Gui/MenuHelper.h>
#include <Modules/Gfx/Ogre/Gui/MenuManager.h>

#include <CEGUIWindowManager.h>
#include <CEGUIEventArgs.h>
#include <elements/CEGUIPushButton.h>
#include <elements/CEGUIListbox.h>
#include <elements/CEGUIListboxTextItem.h>

namespace galaxy {
	namespace gfx {
		namespace ogre3d {
			namespace menu {

				SettingsMenu::Name SettingsMenu::c_MainMenuButton = "MainMenu";
				SettingsMenu::Name SettingsMenu::c_ActionListBox  = "tab/keys/actions";

				bool SettingsMenu::load () {
					LOG_TRACE ("loading menu SettingsMenu");
					m_Window = 
						CEGUI::WindowManager::getSingleton().loadWindowLayout("settings.layout", "", "GUI");
					return false;
				}

				bool SettingsMenu::setup () {
					LOG_TRACE ("setup menu SettingsMenu");

					CEGUI::WindowManager& winMngr = CEGUI::WindowManager::getSingleton();

					gui::MenuHelper::pushButtonClick (&SettingsMenu::MainMenu_OnClick, this, c_MainMenuButton);

					setupKeysTab ();

					CEGUI::System::getSingleton().setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook",
						(CEGUI::utf8*)"MouseArrow");
					const CEGUI::Image * mouseImage = CEGUI::System::getSingleton().getDefaultMouseCursor();
					CEGUI::MouseCursor::getSingleton().setImage(mouseImage);

					return true;
				}

				bool SettingsMenu::unload () {

					if (m_Window) {
						LOG_TRACE ("unloading menu SettingsMenu");
						CEGUI::WindowManager::getSingleton().destroyWindow(m_Window);
						m_Window = 0L;
					}
					return false;
				}

				bool SettingsMenu::setupKeysTab () {
					CEGUI::WindowManager& winMngr = CEGUI::WindowManager::getSingleton();

					CEGUI::Listbox * actionList =
						(CEGUI::Listbox *) winMngr.getWindow(c_ActionListBox);
					//actionList->setSsetSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

					actionList->addItem(new CEGUI::ListboxTextItem("UP"));
					actionList->addItem(new CEGUI::ListboxTextItem("DOWN"));
					actionList->addItem(new CEGUI::ListboxTextItem("LEFT"));
					actionList->addItem(new CEGUI::ListboxTextItem("RIGHT"));

					actionList->subscribeEvent(CEGUI::Listbox::EventSelectionChanged, 
						CEGUI::Event::Subscriber(&SettingsMenu::onKeyActionSelectionChanged, this));

					return true;
				}

				bool SettingsMenu::MainMenu_OnClick(const CEGUI::EventArgs &args) {
					return changeState (logic::State::c_MainMenu);
				}

				bool SettingsMenu::onKeyActionSelectionChanged(const CEGUI::EventArgs &e)
				{
					// The selection has changed.
					CEGUI::Listbox * listBox = static_cast<CEGUI::Listbox*>(CEGUI::WindowManager::getSingletonPtr()->getWindow(c_ActionListBox));
					// Get the item we selected
					CEGUI::ListboxItem * selectedItem = listBox->getFirstSelectedItem();
					selectedItem->setText("Oh we got selected!");

					return true;
				}


			} // namespace menu
		} // namespace ogre3d
	} // namespace gfx
} // namespace galaxy
